package com.czwj.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.Company;
import com.czwj.entities.DataGridData;
import com.czwj.entities.Duty2;
import com.czwj.entities.Duty3;
import com.czwj.entities.Duty4;
import com.czwj.entities.OperateResult;
import com.czwj.entities.PageBean;
import com.czwj.entities.ProjectItemChild;
import com.czwj.entities.Role;
import com.czwj.entities.UserInfo;
import com.czwj.service.Duty2Service;
import com.czwj.service.Duty3Service;
import com.czwj.service.Duty4Service;
import com.czwj.service.DutyChild2Service;
import com.czwj.service.DutyChild3Service;
import com.czwj.service.ProjectItemChildService;
import com.czwj.util.CipherUtil;
import com.czwj.util.TextUtil;

@Controller
public class ProjectItemChildAction{
	@Resource
	ProjectItemChildService projectItemChildService;
	
	@Resource
	DutyChild2Service dutyChild2Service;
	
	@Resource
	DutyChild3Service dutyChild3Service;
	
	@RequestMapping(value = "projectitemchilds",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<ProjectItemChild> projectitemchilds(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "projectitemid",required = false) String projectitemid){
		DataGridData<ProjectItemChild> result = new DataGridData<>();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		if(TextUtil.isNotEmpty(projectitemid)){
			map.put("projectitemid",projectitemid);
		}
		List<ProjectItemChild> list = projectItemChildService.search(map);
		result.setRows(list);
		result.setTotal(projectItemChildService.count(map));
		return result;
	}

	@RequestMapping(value = "/projectitemchildlist", method = RequestMethod.POST)
	@ResponseBody
	public List<ProjectItemChild> projectitemchildlist(@RequestParam(value = "projectitemid",required = false) String projectitemid,
			@RequestParam(value = "status",required = false) String status) {
		Map<String,Object> map = new HashMap<String,Object>();
		if(TextUtil.isNotEmpty(projectitemid)){
			map.put("projectitemid",projectitemid);
		}
		if(TextUtil.isNotEmpty(status)){
			map.put("status",status);
		}
		List<ProjectItemChild> list = projectItemChildService.search(map);
		return list;
	}
	@SystemControllerLog(description = "修改子项目")
	@RequestMapping(value = "/ProjectItemChildSave.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult projectItemChildSave(@RequestBody ProjectItemChild item,HttpServletRequest request)
			throws Exception{
		OperateResult result = new OperateResult();
		int count = 0;
		if(item.getId() != null && item.getId() > 0){
			count = projectItemChildService.update(item);
		}else{
			Subject currentUser = SecurityUtils.getSubject();
			item.setCreaterid(((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId());
			item.setCreatedate(new Date());
			item.setCreateip(request.getRemoteAddr());
			count = projectItemChildService.insert(item);
		}
		result.setSuccess(count > 0);
		return result;
	}
	
	@SystemControllerLog(description = "禁用子项目")
	@RequestMapping(value = "ProjectItemChildDelete.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult ProjectItemChildDelete(@RequestParam(value = "id",required = true) Integer id,
			@RequestParam(value = "status",required = true) Integer status){
		OperateResult result = new OperateResult();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id",id);
		map.put("status",status);
		int count = projectItemChildService.updateStatus(map);
		result.setSuccess(count > 0);
		return result;
	}
	
}
